package engine.graphics.popup;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

import util.List;
import engine.UnitAction;
import engine.data.Unit;
import engine.data.UnitType;
import engine.graphics.MapCanvasEngine;
import engine.graphics.misc.GraphicsCustomFont;

public final class PopupUnitOrders extends TabPopup {

	public boolean selectionMode = false;

	public short selectionX;

	public short selectionY;

	public byte command = UnitAction.NONE;

	private static final byte ORDERS_PANEL = 1;

	private static final byte INFOS_PANEL = 2;

	private static final byte MISC_PANEL = 3;

	private static int[][][] ordersCursor;

	private static int[][][] miscCursor;

	private static String[] miscCommands = { "Show map ...", "Quick save", "Quick load", "Revolution ...", "Taxes ...",
			"Load ...", "Save ...", "Save & Quit", "Quit" };

	private Unit unit;

	private byte vOrderIndex = 0;

	private byte vMiscIndex = 0;

	private List commands;

	private static final int[][][] TAB_CURSOR = { { { 1, 1 }, { 17, 11 } }, { { 20, 1 }, { 33, 11 } },
			{ { 55, 1 }, { 28, 11 } }, { { 85, 1 }, { 23, 11 } } };

	public PopupUnitOrders(final Unit selectedUnit, final List unitCommands) {
		tabCursor = TAB_CURSOR;
		this.unit = selectedUnit;
		this.commands = unitCommands;
		miscCursor = initCursor((byte) miscCommands.length, (byte) 10);
	}

	public void show(final int screenX, final int screenY) {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		super.show(screenX, screenY, graphics);
		GraphicsCustomFont.drawString(graphics, "[X] Orders Infos Misc", this.x + 2, this.y + 3);
		hIndex = ORDERS_PANEL;
		showHCursor();
		showPanel();
	}

	public void showMiscPanel() {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		int j = MARGIN_PANEL_HEIGHT;
		for (byte i = 0; i < miscCommands.length; i++) {
			GraphicsCustomFont.drawString(graphics, miscCommands[i], this.x + 5, this.y + j);
			j += 10;
		}
		this.panel = MISC_PANEL;
	}

	public void showInfosPanel() {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		graphics
				.drawImage(this.unit.getImage(), this.x + 3, this.y + MARGIN_PANEL_HEIGHT, Graphics.TOP | Graphics.LEFT);
		StringBuffer sb = new StringBuffer();
		sb.append(UnitType.getName(unit.type));
		if (unit.veteran)
			sb.append("\nVeteran");
		GraphicsCustomFont.drawString(graphics, sb.toString(), this.x + 25, this.y + MARGIN_PANEL_HEIGHT);
		sb = new StringBuffer();
		sb.append("Movement : ").append(unit.mp / 3).append(".").append(unit.mp % 3).append("\nAttack   : ").append(
				UnitType.getAP(unit.type)).append("\nDefense  : ").append(UnitType.getDP(unit.type)).append(
				"\nRange    : ").append(UnitType.getRP(unit.type));
		if (unit.action != UnitAction.NONE)
			sb.append("\nAction   : ").append(UnitAction.getActionName(unit.action)).append("\n -> ").append(unit.at)
					.append(" turns left");
		if (unit.fortified)
			sb.append("\nState    : Fortified");
		else if (unit.waiting)
			sb.append("\nState    : Waiting");
		else if (unit.sentry)
			sb.append("\nState    : Sentry");
		else if (unit.played)
			sb.append("\nState    : Played");
		else
			sb.append("\nState    : Active");
		GraphicsCustomFont.drawString(graphics, sb.toString(), this.x + 5, this.y + MARGIN_PANEL_HEIGHT + 22);
		this.panel = INFOS_PANEL;
	}

	public void showOrdersPanel() {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		graphics
				.drawImage(this.unit.getImage(), this.x + 3, this.y + MARGIN_PANEL_HEIGHT, Graphics.TOP | Graphics.LEFT);
		ordersCursor = new int[this.commands.size() + 1][2][2];
		String name;
		for (short i = 0; i < ordersCursor.length; i++) {
			if (i == 0)
				name = "Select other unit ...";
			else
				name = UnitAction.getActionName(((Byte) (this.commands.get(i - 1))).byteValue());
			GraphicsCustomFont.drawString(graphics, name, this.x + 25, this.y + MARGIN_PANEL_HEIGHT + (i * 10));
			ordersCursor[i][0][0] = 22;
			ordersCursor[i][0][1] = MARGIN_PANEL_HEIGHT - 1 + (i * 10);
			ordersCursor[i][1][0] = 110;
			ordersCursor[i][1][1] = 10;
		}
		this.panel = ORDERS_PANEL;
	}

	public void keyPressed(final int key) {
		int action = MapCanvasEngine.canvas.getGameAction(key);
		if (action != 0)
			switch (action) {
				case Canvas.DOWN:
					switch (this.panel) {
						case ORDERS_PANEL:
							vOrderIndex = moveCursor(ordersCursor, vOrderIndex, (byte) 1, (byte) 12);
							break;
						case MISC_PANEL:
							vMiscIndex = moveCursor(miscCursor, vMiscIndex, (byte) 1, (byte) 12);
							break;
						default:
							break;
					}
					break;
				case Canvas.UP:
					switch (this.panel) {
						case ORDERS_PANEL:
							vOrderIndex = moveCursor(ordersCursor, vOrderIndex, (byte) -1, (byte) 12);
							break;
						case MISC_PANEL:
							vMiscIndex = moveCursor(miscCursor, vMiscIndex, (byte) -1, (byte) 12);
							break;
						default:
							break;
					}
					break;
				case Canvas.LEFT:
					showNextTab((byte) -1);
					break;
				case Canvas.RIGHT:
					showNextTab((byte) 1);
					break;
				case Canvas.FIRE:
					switch (this.panel) {
						case ORDERS_PANEL:
							if (!hCursorScope)
								if (this.vOrderIndex == 0) {
									selectionMode = true;
									selectionX = this.unit.x;
									selectionY = this.unit.y;
									this.hide();
								} else {
									command = ((Byte) (this.commands.get(this.vOrderIndex - 1))).byteValue();
									this.hide();
								}
							break;
						case MISC_PANEL:
							if (!hCursorScope)
								if (this.vMiscIndex == 0)
									PopupManager.showPopupMap();
							break;
						case CLOSE_PANEL:
							this.hide();
							break;
						default:
							break;
					}
					break;
				default:
					break;
			}
	}

	protected void showPanel() {
		super.showPanel();
		switch (this.hIndex) {
			case ORDERS_PANEL:
				showOrdersPanel();
				break;
			case MISC_PANEL:
				showMiscPanel();
				break;
			case INFOS_PANEL:
				showInfosPanel();
				break;
			default:
				break;
		}
		this.repaint();
	}
}
